/**
 * @projectName demo
 * @package com.ws.demo.test
 * @className com.ws.demo.test.LocalDateTest
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package com.tyk.demo.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * LocalDateTest
 * @description LocalDate测试类
 * @author tuoyangkun
 * @date 2020/6/11 14:13
 * @version 1.0
 */
public class LocalDateUtil {
	/**
	  * LocalDateUtil
	  * @description
	  * @param dateStr: 日期字符串（如"2020-06-10"）
	  * @param formatText: 日期字符串的格式（如"yyyy-MM-dd"）
	  * @return: java.time.LocalDate
	  * @date 2020/6/11 14:54
	  * @author tuoyangkun
	  * @version 1.0.0
	  */
	public static LocalDate string2LocalDate(String dateStr,String formatText) {
		DateTimeFormatter formatter;
		if (formatText == null) {
			formatText = "yyyy-MM-dd";
		}
		formatter = DateTimeFormatter.ofPattern(formatText);
		return LocalDate.parse(dateStr,formatter);
	}

	/**
	  * LocalDateUtil
	  * @description String转LocalDateTime
	  * @param dateTimeStr:
	  * @param formatText:
	  * @return: java.time.LocalDateTime
	  * @date 2020/6/24 17:12
	  * @author tuoyangkun
	  * @version 1.0.0
	  */
	public static LocalDateTime string2LocalDateTime(String dateTimeStr,String formatText) {
		DateTimeFormatter formatter;
		if (formatText == null) {
			formatText = "yyyy-MM-dd HH:mm:ss";
		}
		formatter = DateTimeFormatter.ofPattern(formatText);
		return LocalDateTime.parse(dateTimeStr,formatter);
	}

	/**
	  * LocalDateUtil
	  * @description Date转LocalDate
	  * @param date:
	  * @return: java.time.LocalDate
	  * @date 2020/6/11 14:54
	  * @author tuoyangkun
	  * @version 1.0.0
	  */
	public static LocalDate date2LocalDate(Date date) {
		// 获取时间实例
		Instant instant = date.toInstant();
		// 获取时间地区ID
		ZoneId zoneId = ZoneId.systemDefault();
		// 转换为LocalDate
		return instant.atZone(zoneId).toLocalDate();
	}

	/**
	  * LocalDateUtil
	  * @description
	  * @param localDate:
	  * @param formatText:
	  * @return: java.lang.String
	  * @date 2020/6/16 16:27
	  * @author tuoyangkun
	  * @version 1.0.0
	  */
	public static String localDate2String(LocalDate localDate,String formatText) {
		if (formatText == null) {
			formatText = "yyyy-MM-dd";
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatText);
		return localDate.format(formatter);
	}

	public static String localDateTime2String(LocalDateTime localDateTime,String formatText) {
		if (formatText == null) {
			formatText = "yyyy-MM-dd HH:mm:ss";
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatText);
		return localDateTime.format(formatter);
	}
}
